package com.liyong.bos.service;

import com.liyong.bos.domain.WeatherGeneric;
import com.liyong.bos.domain.WeatherInfo;
import com.liyong.bos.webxml.ArrayOfString;
import com.liyong.bos.webxml.WeatherWebService;
import com.liyong.bos.webxml.WeatherWebServiceSoap;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ApacheMoy on 2017/4/20.
 */
@Service
public class GetWeatherService {
    public WeatherInfo getWeatherByCityName(String cityName) {
        //创建一个WeatherWS工厂
        WeatherWebService factory = new WeatherWebService();
        //根据工厂创建一个WeatherWSSoap对象
        WeatherWebServiceSoap weatherWSSoap = factory.getWeatherWebServiceSoap();
        //调用WebService提供的getWeather方法获取天气预报情况
        ArrayOfString weatherInfo = weatherWSSoap.getWeatherbyCityName(cityName);
        List<String> lstWeatherInfo = weatherInfo.getString();
        WeatherInfo weather=new WeatherInfo();
        List<WeatherGeneric> weatherGenerics=null;
        WeatherGeneric weatherGeneric=null;
        /**
         * 此做法旨在防止因远程访问内容发生改变时造成客户端程序异常的情况
         * 如果是服务端出现异常，则可以及时做出调整
         * */
        for(int i=0;i<lstWeatherInfo.size();i++){
            if(i>=12)
                break;
            switch (i){
                case 0:
                    weather.setProvince(lstWeatherInfo.get(i));
                    break;
                case 1:
                    weather.setCityName(lstWeatherInfo.get(i));
                    break;
                case 4:
                    weather.setNowDate(lstWeatherInfo.get(i));
                    break;
                case 5:
                    weatherGenerics= new ArrayList<WeatherGeneric>();
                    weatherGeneric=new WeatherGeneric();
                    weatherGeneric.setTemperature(lstWeatherInfo.get(i));
                    break;
                case 6:
                    weatherGeneric.setWeather(lstWeatherInfo.get(i));
                    break;
                case 7:
                    weatherGeneric.setWindPower(lstWeatherInfo.get(i));
                    break;
                case 8:
                    weatherGeneric.setImgName1(lstWeatherInfo.get(i));
                    break;
                case 9:
                    weatherGeneric.setImgName2(lstWeatherInfo.get(i));
                    weatherGenerics.add(weatherGeneric);
                    break;
                case 10:
                    weather.setCurrentWeaher(lstWeatherInfo.get(i));
                    break;
                case 11:
                    String str=lstWeatherInfo.get(i);
                    int coldIndex=str.indexOf("感冒指数");
                    int wearIndex=str.indexOf("穿衣指数");
                    int sportIndex=str.indexOf("运动指数");
                    int airIndex=str.indexOf("空气污染指数");
                    weather.setColdIndex(str.substring(coldIndex,wearIndex-1));
                    weather.setWearIndex(str.substring(wearIndex,sportIndex-1));
                    weather.setSportIndex(str.substring(sportIndex,airIndex-1));
                    weather.setAirIndex(str.substring(airIndex,str.length()-1));
                    weatherGeneric=null;
                    break;
            }
        }
        for(int i=12;i<lstWeatherInfo.size();i++){
            switch (i){
                case 12:
                    weatherGeneric=new WeatherGeneric();
                    weatherGeneric.setTemperature(lstWeatherInfo.get(i));
                    break;
                case 13:
                    weatherGeneric.setWeather(lstWeatherInfo.get(i));
                    break;
                case 14:
                    weatherGeneric.setWindPower(lstWeatherInfo.get(i));
                    break;
                case 15:
                    weatherGeneric.setImgName1(lstWeatherInfo.get(i));
                    break;
                case 16:
                    weatherGeneric.setImgName2(lstWeatherInfo.get(i));
                    weatherGenerics.add(weatherGeneric);
                    weatherGeneric=null;
                    break;
                case 17:
                    weatherGeneric=new WeatherGeneric();
                    weatherGeneric.setTemperature(lstWeatherInfo.get(i));
                    break;
                case 18:
                    weatherGeneric.setWeather(lstWeatherInfo.get(i));
                    break;
                case 19:
                    weatherGeneric.setWindPower(lstWeatherInfo.get(i));
                    break;
                case 20:
                    weatherGeneric.setImgName1(lstWeatherInfo.get(i));
                    break;
                case 21:
                    weatherGeneric.setImgName2(lstWeatherInfo.get(i));
                    weatherGenerics.add(weatherGeneric);
                    weatherGeneric=null;
                    break;
            }
        }
        weather.setWeatherGenerics(weatherGenerics);
        return  weather;
    }
}
